package com.lloydfinch.composetour

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.doAfterTextChanged
import com.lloydfinch.composetour.databinding.ActivityAndroidMainBinding
import com.lloydfinch.composetour.vm.VMName

class AndroidMainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityAndroidMainBinding
    private val vmName by viewModels<VMName>()

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityAndroidMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.txtName.doAfterTextChanged {
            vmName.onNameChanged(it.toString())
        }

        vmName.name.observe(this) { name ->
            binding.tvName.text = "Hello $name"
        }
    }
}